import {
  selectList,
  selectDetails,
  rechange,
  oilCardOperate,
  ableOilCardOperate,
  deleteOilCard,
  selectMasterCardList,
} from '@/services/tmsOilCard';
import { memberExpression } from '@babel/types';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'tmsOilCard',

  state: {
    selectList: {},
    selectDetails: {},
    rechargeList: {},
    papersList: {},
  },

  effects: {
    *selectList({ payload, callback }, { call, put }) {
      const response = yield call(selectList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        callback && callback(list);
        yield put({
          type: 'show',
          payload: {
            selectList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
      }
    },
    *selectMasterCardList({ payload, callback }, { call }) {
      const response = yield call(selectMasterCardList, payload);
      if (response.code === 0) {
        callback(response.data);
      }
    },
    *selectDetails({ payload, callback }, { call, put }) {
      const response = yield call(selectDetails, payload);

      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            selectDetails: { [payload.id]: response.data },
            rechargeList: { [payload.id]: response.data.details },
          },
        });
        callback && callback(response.data);
      }
    },
    *oilCardOperate({ payload, callback }, { call }) {
      const response = yield call(oilCardOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        console.log('callback', callback);
        if (callback) callback(response.data);
      }
    },
    *ableOilCardOperate({ payload, callback }, { call }) {
      const response = yield call(ableOilCardOperate, payload);
      const message = payload.type ? `启用${response.message}` : `禁用${response.message}`;
      if (response.code === 0) {
        Prompt({ content: response.message });
        callback(response);
      }
    },
    // 油卡充值
    *rechange({ payload, callback }, { call, put }) {
      const response = yield call(rechange, payload);
      if (response.code === 0) {
        Prompt({ content: '充值成功' });
        callback(response);
      }
    },
    //删除：
    *deleteOilCard({ payload, callback }, { call, put }) {
      const response = yield call(deleteOilCard, payload);
      if (response.code === 0) {
        Prompt({ content: `删除${response.message}` });
        callback(response);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        selectList: payload.selectList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        selectDetails: { ...state.selectDetails, ...payload.selectDetails },
        rechargeList: { ...state.rechargeList, ...payload.rechargeList },
      };
    },
  },
};
